package com.gs.weather.service.impl;

import com.gs.weather.dao.DisasterTypeDao;
import com.gs.weather.entity.DisasterType;
import com.gs.weather.service.DisasterTypeService;
import com.gs.weather.utils.HttpUtils;
import com.gs.weather.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.List;

import static com.gs.weather.utils.Constant.PAGE_SIZE;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/9/21 0021
 */
@Service
public class DisasterTypeServiceImpl implements DisasterTypeService {
    @Autowired
    DisasterTypeDao disasterTypeDao;

    @Override
    public Integer addDisasterType(DisasterType disasterType) {
        return  disasterTypeDao.addDisasterType(disasterType);
    }

    @Override
    public ResponseEntity updateDisasterType(DisasterType disasterType) {
        disasterTypeDao.updateDisasterType(disasterType);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }

    //PageHelper.orderBy("blog_ID desc");
    @Override
    public PageBean getAllDisasterType(Integer pageNum) {
        pageNum = pageNum == null?1:pageNum;
        int count = disasterTypeDao.getTypeCount();
        int totalPage = count/PAGE_SIZE+(count%PAGE_SIZE==0?0:1);
        List<DisasterType> types =disasterTypeDao.queryAllDisasterType(pageNum);
        PageBean<DisasterType> result = new PageBean(pageNum,totalPage,types);
        return result;
    }

    @Override
    public ResponseEntity deleteDisasterType(int id) {
        disasterTypeDao.deleteDisasterType(id);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }
}
